<?php
class BaseDelegator
{
	public function __construct() {}
	public function byDepartment(EFacade & $facade, $currentTransSet) 
	{	
		$ezpdo_ = epManager::instance();
		

		$queryDep='FROM MDepartamento as t WHERE t.identificadorUnico=?';
		$doc = & $facade->getDocumentRoot();

		$usuarioActual=$doc->usuarioActual;
		$idDep=$doc->departamento->identificadorUnico;
		$variante=$doc->variante;
		$workflow = & $facade->getWorkflow();
		$areaTrabajo=NULL;
		$role=NULL;
		$cont=0;
		

		$currentPlaceSet=$facade->getplaceSet();
		/**
		 *TODO: encontrar el Place Set asociado al transSet
		 * 
		 */ 
		
		$dptoVarietyList=$currentPlaceSet[0]->dptoVarietyList;
		


		foreach($dptoVarietyList as $dv){

			foreach($dv->variantList as $v){
				
				if($v->epGetObjectId()==$variante->epGetObjectId()){
					
					$departamento1=$ezpdo_->find($queryDep,$dv->dptoName);
					$doc->departamento=$departamento1[0];
					$doc->variante=$v;
				}
			}
		}

		$role=$currentTransSet->roleRef;
		$roleId = $role->epGetObjectId();
		/**
		 * TODO mejorar la pregunta
		 */

	

		$query = 'FROM MUser as u WHERE u.perfilList.contains(p) AND p.roleList.contains(?) '; // la pregunta esta mal hecha AND u.empleado.departamento.oid=?

		$users = $ezpdo_->find($query, $role,$doc->departamento->epGetObjectId());
		

		$oldUser = NULL;
		foreach($workflow->history->history as $history) {

			if($history->userRef && $history->firedTransition->roleRef->epGetObjectId() == $roleId){
				foreach($users as & $user) {
					if($user->epGetObjectId() == $history->userRef->epGetObjectId() ) {
						$oldUser = $user; 
						break;
					}
				}
			}
		}
	if($oldUser) return $oldUser;
	/**
	 * TODO: Incorporar la asignacion de menos cargado 
	 */
	return $users[0];
	}
		
	public function porTurno(EFacade & $facade,$transSet) 
	{	
		$ezpdo_ = & getManager();
		$queryDep='FROM MDepartamento as t WHERE t.identificadorUnico=?';
		$doc = & $facade->getDocumentRoot();
		$usuarioActual=$doc->usuarioActual;
		$idDep=$doc->departamento->identificadorUnico;
		$variante=$doc->variante;
		$workflow= & $facade->getWorkflow();
		$areaTrabajo=NULL;
		$role=NULL;
		$cont=0;
	    $currentTransSet=$facade->getCurrentTransSet($workflow,$role);
		$currentPlaceSet=$facade->getCurrentPlaceSet($workflow);
		$dptoVarietyList=$currentPlaceSet->dptoVarietyList;
		foreach($dptoVarietyList as $dv){
			
			foreach($dv->variantList as $v){
				if($v->epGetObjectId()==$variante->epGetObjectId()){
					
					
					$departamento1=$ezpdo_->find($queryDep,$dv->dptoName);
					$doc->departamento=$departamento1[0];
					$doc->variante=$v;
				}
			}
			
		}
		$role=$currentTransSet->transList[0]->roleRef;

		
		$query = 'FROM MUser as u WHERE u.perfilList.contains(p) AND p.roleList.contains(?) AND u.empleado.departamento=?';
		$users = $ezpdo_->find($query, $role,$doc->departamento);
		
			
		//$users = $session->find($query, $role,$areaTrabajo);
		$ret=NULL;
		foreach ($users as & $user) {
			if ($ret == null) {
				$ret = & $user;
			}

			if ($user->getActorList()->count() < $ret->getActorList()->count()) {
				$ret = & $user;
			}
		}
		
		$workTransList= & $workflow->workTransList;
		$transList=$currentTransSet->transList;
		foreach($workTransList as $wt){
			
			foreach($transList as $t){
				
				if($wt->transRef->epGetObjectId()==$t->epGetObjectId()){
					if($wt->actorRef){
						$wt->actorRef->userRef=$ret;
					}else{
						$actor=$ezpdo_->create('MActor');
						$actor->userRef=$ret;
						$actor->workflowRef=$workflow;
						$wt->actorRef=$actor;
						
					}
				}
			}
		}
	}
	
	public function sameUserByRol(& $facade, $transSet) {
		$ezpdo_ = epManager::instance();
		$doc = & $facade->getDocumentRoot();
		$workflow = $doc->workflowRef;
		$role = $transSet->roleRef;
		$oldUser = NULL;
		foreach($workflow->history->history as $elem) {
			if($elem->firedTransition->roleRef->epGetObjectId() == $role->epGetObjectId()) {
				$oldUser = $elem->userRef;
				break;
			}
		}
		if($oldUser) {
			return $oldUser;
		}
		$query =  'FROM MUser as u WHERE u.perfilList.contains(p) AND p.roleList.contains(?)';
		$users = $ezpdo_->find($query,$role);
		
		
	}
	
	public function sameUserByRolDept(& $facade, $transSet) {
		
	}
	
	public function timer(EFacade & $facade, $transSet) {

	// solo puede haber una transicion
		$fechaBase = & $facade->getDocumentRoot()->fecha;
		$retardo =  $transSet->transList[0]->time;
		if($retardo == 2) {
			if(date('w', $fechaBase) < 4) $fechaDisparo = $fechaBase+24*2*3600;
			else $fechaDisparo = $fechaBase+24*4*3600;
		}
		else {
			$fechaDisparo = $fechaBase + 24*14*3600;
		}
		return $fechaDisparo;
		
	}
}
?>
